package com.frank.code_notes.C01_array;


import cn.hutool.core.lang.Console;

/**
 * @Description 34. 在排序数组中查找元素的第一个和最后一个位置
 * @Author zhoujr
 * @Data 2024/11/24 19:55
 */
public class L034ArrayFindFirstAndLast {

    /**
     * @Description 分别寻找左右边界
     */
    public static int[] searchRange(int[] nums, int target) {
        int leftBorder = searchRangeLeft(nums, target);
        int rightBorder = searchRangeRight(nums, target);
        if (leftBorder == -2 || rightBorder == -2) return new int[]{-1, -1};
        if (rightBorder - leftBorder > 1) return new int[]{leftBorder + 1, rightBorder - 1};
        return new int[]{-1, -1};
    }

    /**
     * @Description 查找左边界
     */
    public static int searchRangeLeft(int[] nums, int target) {
        int left = 0, right = nums.length - 1, rightBorder = -2;
        while (left <= right) {
            int mid = left + ((right - left) >> 1);
            if (nums[mid] >= target) { // 寻找左边界，nums[middle] == target的时候更新right
                right = mid - 1;
                rightBorder = right;
            } else {
                left = mid + 1;
            }
        }
        return rightBorder;
    }

    /**
     * @Description 查找右边界
     */
    public static int searchRangeRight(int[] nums, int target) {
        int left = 0, right = nums.length - 1, rightBorder = -2;
        while (left <= right) {
            int mid = left + ((right - left) >> 1);
            if (nums[mid] > target) {
                right = mid - 1;
            } else { // 寻找右边界，nums[middle] == target的时候更新left
                left = mid + 1;
                rightBorder = left;
            }
        }
        return rightBorder;
    }

    /**
     * @Description 先使用二分法寻找目标值位置，再分别向左右滑动寻找边界
     */
    public static int[] searchRangeTwo(int[] nums, int target) {
        int i = binarySearch(nums, target);
        if (i == -1) return new int[]{-1, -1};
        int left = i, right = i;
        while (left - 1 >= 0 && nums[left - 1] == target) {
            left--;
        }

        while (right + 1 < nums.length && nums[right + 1] == target) {
            right++;
        }

        return new int[]{left, right};
    }

    public static int binarySearch(int[] nums, int target) {
        int left = 0, right = nums.length;
        while (left < right) {
            int mid = left + ((right - left) >> 1);
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] > target) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        Console.log(searchRange(new int[]{2,2}, 2));
        Console.log(searchRange(new int[]{5, 7, 7, 8, 8, 10}, 6));
        Console.log(searchRange(new int[]{5, 7, 7, 8, 8, 10}, 8));
        Console.log(searchRangeTwo(new int[]{2,2}, 2));
        Console.log(searchRangeTwo(new int[]{5, 7, 7, 8, 8, 10}, 6));
        Console.log(searchRangeTwo(new int[]{5, 7, 7, 8, 8, 10}, 8));
    }

}
